import { logoutUser } from "../utils/login";

// 统一接口返回
export interface ITResponseData {
    // 返回状态
    success: boolean;

    // 错误信息
    message: string;

    // 返回数据
    data: any;

    // 返回码
    code: number;
}

interface ITResponse {
    data: any;
    status: number;
    code?: any;
}

// 通用接口校验
export const checkResonse = (res: ITResponse): boolean => {
    if (!res) return false;
    const { status, code: resCode } = res;
    if (resCode === 501) {
        logoutUser();
        return false;
    }
    if (status === 401) {
        logoutUser();
        return false;
    }
    const result: ITResponseData = res.data;
    const { success, code } = result || {};
    if (status === 200 && success && code === 200) {
        return true;
    } else {
        return false;
    }
};
